﻿using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web.Script.Serialization;
using Microsoft.IdentityModel.Web;

namespace LogOnWithACSLibrary
{
    /// <summary>
    /// Abstracts the acquisition of the HRD(Home Realm Discovery) JSON Feed
    /// </summary>
    public class HrdClient
    {
        public HrdClient()
        {
        }

        public virtual IEnumerable<HrdIdentityProvider> GetHrdResponse(string returnUrl)
        {
            WSFederationAuthenticationModule fam = FederatedAuthentication.WSFederationAuthenticationModule;
            HrdRequest request = new HrdRequest(fam.Issuer, fam.Realm, context: returnUrl);

            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;

            string response = client.DownloadString(request.GetUrlWithQueryString());

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<List<HrdIdentityProvider>>(response);
        }
    }
}
